$(function(){
console.log("页面加载完成事件");
/**
 * 条件：审核通过
 * @returns
 */
	/*页面一加载，执行*/
	display(1,5);
	/*点击查询按钮*/
	$("#query-action-btn").click(function(){
console.log("分页----点击搜索按钮");
		display(1,$("#query-page").bs_pagination('getOption','rowsPerPage'));
	});
	/*点击查询全部按钮*/
	$("#query-action-btn-all").click(function(){
		location.href="Registration_information_audit.html"
	});
	//函数
	function display(pageNoStr,pageSizeStr){
		/*查询条件*/
		var unit = $("#unit").val();
		var name=$("#name").val();
		var phone = $("#phone").val();
		var state = $("#state option:selected").val();
		var stateTime = $("#stateTime").val();
		var endTime = $("#endTime").val();
		/*执行ajax*/
		$.ajax({
			url:"Register/findRegistryAudit",
			data:{
				pageNoStr:pageNoStr,
				pageSizeStr:pageSizeStr,
				unit:unit,
				name:name,
				phone:phone,
				state:state,
				stateTime:stateTime,
				endTime:endTime
			},
			type:"post",
			dataType:"json",
			success:function(map){
				var htmlStr="";
				var registryList = map.registerList;
				for( var i = 0; i < registryList.length; i++ ){
	htmlStr+="<tr align='center'>"
	if( registryList[i].state == 0 ){
		htmlStr+="<td><input type='checkbox' name='id' value='"+registryList[i].id+"' ></td>"
	} else{
		htmlStr+="<td></td>"
	}	
	htmlStr+="<td>"+(i+1)+"</td>"
	htmlStr+="<td>"+registryList[i].unit+"</td>"
	htmlStr+="<td>"+registryList[i].name+"</td>"
	htmlStr+="<td>"+registryList[i].phone+"</td>"
	htmlStr+="<td>"+registryList[i].enterTime+"</td>"
	var address = registryList[i].live.province+registryList[i].live.city+registryList[i].live.county;
	htmlStr+="<td>"+address+"</td>"
	htmlStr+="<td>"+registryList[i].permissions.permissionstype+"</td>"
	var state = registryList[i].state;
	if( state == 0 ){
	htmlStr+="<td>未审核</td>"
	}else if( state == 1 ){
	htmlStr+="<td>通过</td>"
	} else if( state == 2 ){
	htmlStr+="<td>未通过</td>"
	}
	htmlStr+="</tr>"
				}
				//展示到页面上
				$("tbody").html(htmlStr);
				/*页面插件-分页展示*/
				//计算总页数
				var totalPages =1;
				if( map.count%pageSizeStr == 0 ){
					totalPages = map.count/pageSizeStr;
				}else{
					totalPages = parseInt(map.count/pageSizeStr)+1;
				}
				$("#query-page").bs_pagination({
					currentPage:pageNoStr,	//当前页码
					rowsPerPage:pageSizeStr,	//每页显示条数
					totalRows:map.count,	//总条数
				    totalPages: totalPages,//总页数 
				    visiblePageLinks:5,	//最多显示的卡片数
				    showGoToPage:false,		//是否显示跳转到几页
				    showRowsPerPage:false,	//是否显示每页显示条数
				    showRowsInfo:false,		//是否显示记录条数
				    //用来监听页号切换的事件，event代表事件，pageobj代表页面信息
				    onChangePage:function(event,pageObj){
				    	display(pageObj.currentPage,pageObj.rowsPerPage);
				    } 
				});
			},
			error:function(){
				location.href='404.html'
			}		
		});
	}
	
	/**
	 * 给全选的复选框添加事件
	 */
    $("#all").click(function(){
        // this 全选的复选框
        var userids=this.checked;
        //获取name=box的复选框 遍历输出复选框
        $("input[name=id]").each(function(){
            this.checked=userids;
        });
    });
	
    
	/**
	 * 改变审核状态码-把未审核改为审核通过
	 */
	$("#btn_through").click(function(){
		var ckdids = $("input[name='id']:checked");
		if( ckdids.size() == 0 ){
			alert("请选择要通过的记录");
			return;
		}
		var id = "";
		$.each(ckdids,function(index,obj){
			id += obj.value +",";
		})
		var state = 1;
		$.ajax({
			"url" : "Register/updateRegistry",
			"data" : {
				ids:id,
				state:state
			},
			"type" : "post",
			"dataType" : "json",
			"success" : function(json) {
				console.log("审核状态修改成功");
				location.href='Registration_information_audit.html'
			},
			"error":function(){
				console.log("通过的error");
				location.href='404.html'
			}
		});
	})
	
	/**
	 * 修改审核状态--把审核中改为未通过
	 */
	$("#btn_not_through").click(function(){
		var ckdids = $("input[name='id']:checked");
		if( ckdids.size() == 0 ){
			alert("请选择要改变不通过的记录");
			return;
		}
		var id = "";
		$.each(ckdids,function(index,obj){
			id += obj.value +",";
		})
		var state = 2;
		$.ajax({
			"url" : "Register/updateRegistry",
			"data" : {
				ids:id,
				state:state
			},
			"type" : "post",
			"dataType" : "json",
			"async" : "true",
			"success" : function(json) {
				console.log("审核状态修改成功");
				location.href='Registration_information_audit.html'
			},
			"error":function(){
				console.log("不通过的error");
				location.href='404.html'
			}
		});
	})
})